/// 评论模型
class CommentModel {
  final String id;
  final String postId;
  final String userId;
  final String? parentId;
  final String content;
  final String status;
  final int likesCount;
  final bool isLiked;
  final DateTime createTime;
  final DateTime? updateTime;
  final String? createBy;
  final String? updateBy;
  final bool isDeleted;
  final String? remark;
  
  // 用户信息
  final String username;
  final String? userAvatar;
  
  // 子评论列表
  final List<CommentModel> replies;

  CommentModel({
    required this.id,
    required this.postId,
    required this.userId,
    this.parentId,
    required this.content,
    required this.status,
    required this.likesCount,
    required this.isLiked,
    required this.createTime,
    this.updateTime,
    this.createBy,
    this.updateBy,
    required this.isDeleted,
    this.remark,
    required this.username,
    this.userAvatar,
    this.replies = const [],
  });

  factory CommentModel.fromJson(Map<String, dynamic> json) {
    return CommentModel(
      id: json['id']?.toString() ?? '',
      postId: json['postId']?.toString() ?? '',
      userId: json['userId']?.toString() ?? '',
      parentId: json['parentId']?.toString(),
      content: json['content'] ?? '',
      status: json['status'] ?? 'active',
      likesCount: json['likesCount'] ?? 0,
      isLiked: json['isLiked'] ?? false,
      createTime: json['createTime'] != null 
          ? DateTime.parse(json['createTime']) 
          : DateTime.now(),
      updateTime: json['updateTime'] != null 
          ? DateTime.parse(json['updateTime']) 
          : null,
      createBy: json['createBy'],
      updateBy: json['updateBy'],
      isDeleted: json['isDeleted'] ?? false,
      remark: json['remark'],
      username: json['userNickname'] ?? json['username'] ?? '匿名用户',
      userAvatar: json['userAvatar'],
      replies: (json['replies'] as List<dynamic>?)
          ?.map((reply) => CommentModel.fromJson(reply))
          .toList() ?? [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'postId': postId,
      'userId': userId,
      'parentId': parentId,
      'content': content,
      'status': status,
      'likesCount': likesCount,
      'isLiked': isLiked,
      'createTime': createTime.toIso8601String(),
      'updateTime': updateTime?.toIso8601String(),
      'createBy': createBy,
      'updateBy': updateBy,
      'isDeleted': isDeleted,
      'remark': remark,
      'username': username,
      'userAvatar': userAvatar,
      'replies': replies.map((reply) => reply.toJson()).toList(),
    };
  }

  CommentModel copyWith({
    String? id,
    String? postId,
    String? userId,
    String? parentId,
    String? content,
    String? status,
    int? likesCount,
    bool? isLiked,
    DateTime? createTime,
    DateTime? updateTime,
    String? createBy,
    String? updateBy,
    bool? isDeleted,
    String? remark,
    String? username,
    String? userAvatar,
    List<CommentModel>? replies,
  }) {
    return CommentModel(
      id: id ?? this.id,
      postId: postId ?? this.postId,
      userId: userId ?? this.userId,
      parentId: parentId ?? this.parentId,
      content: content ?? this.content,
      status: status ?? this.status,
      likesCount: likesCount ?? this.likesCount,
      isLiked: isLiked ?? this.isLiked,
      createTime: createTime ?? this.createTime,
      updateTime: updateTime ?? this.updateTime,
      createBy: createBy ?? this.createBy,
      updateBy: updateBy ?? this.updateBy,
      isDeleted: isDeleted ?? this.isDeleted,
      remark: remark ?? this.remark,
      username: username ?? this.username,
      userAvatar: userAvatar ?? this.userAvatar,
      replies: replies ?? this.replies,
    );
  }

  /// 获取时间显示文本
  String get timeAgo {
    final now = DateTime.now();
    final difference = now.difference(createTime);

    if (difference.inDays > 0) {
      return '${difference.inDays}天前';
    } else if (difference.inHours > 0) {
      return '${difference.inHours}小时前';
    } else if (difference.inMinutes > 0) {
      return '${difference.inMinutes}分钟前';
    } else {
      return '刚刚';
    }
  }

  /// 是否为回复评论
  bool get isReply => parentId != null && parentId!.isNotEmpty;
}
